package com.rome.hotel.data.service.impl;

import java.util.List;
import com.rome.hotel.common.utils.DateUtils;
import com.rome.hotel.data.domain.HotelReserve;
import com.rome.hotel.data.mapper.HotelReserveMapper;
import com.rome.hotel.data.utils.RegexUtils;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rome.hotel.data.mapper.HotelRoomMapper;
import com.rome.hotel.data.domain.HotelRoom;
import com.rome.hotel.data.service.IHotelRoomService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 房间Service业务层处理
 * 
 * @author rome
 * @date 2023-12-22
 */
@Service
public class HotelRoomServiceImpl implements IHotelRoomService 
{
    @Autowired
    private HotelRoomMapper hotelRoomMapper;

    @Autowired
    private HotelReserveMapper hotelReserveMapper;

    /**
     * 查询房间
     * 
     * @param roomId 房间主键
     * @return 房间
     */
    @Override
    public HotelRoom selectHotelRoomByRoomId(Long roomId)
    {
        return hotelRoomMapper.selectHotelRoomByRoomId(roomId);
    }

    /**
     * 查询房间列表
     * 
     * @param hotelRoom 房间
     * @return 房间
     */
    @Override
    public List<HotelRoom> selectHotelRoomList(HotelRoom hotelRoom)
    {
        return hotelRoomMapper.selectHotelRoomList(hotelRoom);
    }

    /**
     * 新增房间
     * 
     * @param hotelRoom 房间
     * @return 结果
     */
    @Override
    public int insertHotelRoom(HotelRoom hotelRoom)
    {
//        防御性编程
        /**
         * 验证房间号房间号不能超过50位，非空
         * 验证房间名称不能超过50位，非空
         * 房间类型和所属楼层不能超过50位，非空
         * 床型不能超过50位，非空
         * 房间价格只能是数字，非空
         * 折扣价只能是数字，非空
         */
        Assert.notNull(hotelRoom,"参数异常");
        Assert.notNull(hotelRoom.getRoomTypeId(),"房间类型不能为空");
        Assert.notNull(hotelRoom.getFloorId(),"所属楼层不能为空");
        Assert.notNull(hotelRoom.getRoomNumber(),"房间号不能为空");
        Assert.notNull(hotelRoom.getRoomName(),"房间名称不能为空");
        Assert.notNull(hotelRoom.getBedType(),"床型不能为空");
        Assert.notNull(hotelRoom.getRoomPrice(),"房间价格不能为空");
        Assert.notNull(hotelRoom.getRoomDiscountPrice(),"折后价不能为空");
        Assert.state(RegexUtils.testStringNoWuShi(hotelRoom.getRoomNumber()),"房间号不能超过50位");
        Assert.state(hotelRoomMapper.selectHotelRoomByRoomNumber(hotelRoom.getRoomNumber())==null,"房间号已存在");
        Assert.state(RegexUtils.testStringNoWuShi(hotelRoom.getRoomName()),"房间名称不能超过50位");
        Assert.state(RegexUtils.testStringNoWuShi(hotelRoom.getRoomTypeId().toString()),"房间类型不能超过50位");
        Assert.state(RegexUtils.testStringNoWuShi(hotelRoom.getFloorId().toString()),"所属楼层不能超过50位");
        Assert.state(RegexUtils.testNumber(hotelRoom.getRoomTypeId().toString()),"房间类型参数异常，非数字");
        Assert.state(RegexUtils.testNumber(hotelRoom.getFloorId().toString()),"所属楼层参数异常，非数字");
        Assert.state(RegexUtils.testStringNoWuShi(hotelRoom.getBedType()),"床型不能超过50位");
        Assert.state(RegexUtils.testNumber(hotelRoom.getRoomPrice().toString()),"房间价格只能是数字");
        Assert.state(RegexUtils.testNumber(hotelRoom.getRoomDiscountPrice().toString()),"折后价只能是数字");
        Assert.state(hotelRoom.getRoomPrice().compareTo(hotelRoom.getRoomDiscountPrice())>=0,"折扣价要小于原价");
        hotelRoom.setCreateTime(DateUtils.getNowDate());
        return hotelRoomMapper.insertHotelRoom(hotelRoom);
    }

    /**
     * 修改房间
     * 
     * @param hotelRoom 房间
     * @return 结果
     */
    @Override
    public int updateHotelRoom(HotelRoom hotelRoom)
    {
        //        防御性编程
        /**
         * 验证房间号房间号不能超过50位，非空
         * 验证房间名称不能超过50位，非空
         * 房间类型和所属楼层不能超过50位，非空
         * 床型不能超过50位，非空
         * 房间价格只能是数字，非空
         * 折扣价只能是数字，非空
         */
        Assert.notNull(hotelRoom,"参数异常");
        Assert.notNull(hotelRoom.getRoomId(),"房间类型不能为空");
        Assert.notNull(hotelRoom.getRoomTypeId(),"房间类型不能为空");
        Assert.notNull(hotelRoom.getFloorId(),"所属楼层不能为空");
        Assert.notNull(hotelRoom.getRoomNumber(),"房间号不能为空");
        Assert.notNull(hotelRoom.getRoomName(),"房间名称不能为空");
        Assert.notNull(hotelRoom.getBedType(),"床型不能为空");
        Assert.notNull(hotelRoom.getRoomPrice(),"房间价格不能为空");
        Assert.notNull(hotelRoom.getRoomDiscountPrice(),"折后价不能为空");
        Assert.state(RegexUtils.testStringNoWuShi(hotelRoom.getRoomNumber()),"房间号不能超过50位");
        Assert.state(RegexUtils.testStringNoWuShi(hotelRoom.getRoomName()),"房间名称不能超过50位");
        Assert.state(RegexUtils.testStringNoWuShi(hotelRoom.getRoomTypeId().toString()),"房间类型不能超过50位");
        Assert.state(RegexUtils.testStringNoWuShi(hotelRoom.getFloorId().toString()),"所属楼层不能超过50位");
        Assert.state(RegexUtils.testNumber(hotelRoom.getRoomTypeId().toString()),"房间类型参数异常，非数字");
        Assert.state(RegexUtils.testNumber(hotelRoom.getFloorId().toString()),"所属楼层参数异常，非数字");
        Assert.state(RegexUtils.testStringNoWuShi(hotelRoom.getBedType()),"床型不能超过50位");
        Assert.state(RegexUtils.testNumber(hotelRoom.getRoomPrice().toString()),"房间价格只能是数字");
        Assert.state(RegexUtils.testNumber(hotelRoom.getRoomDiscountPrice().toString()),"折后价只能是数字");
        Assert.state(hotelRoom.getRoomPrice().compareTo(hotelRoom.getRoomDiscountPrice())>=0,"折扣价要小于原价");
        return hotelRoomMapper.updateHotelRoom(hotelRoom);
    }

    /**
     * 批量删除房间
     * 
     * @param roomIds 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteHotelRoomByRoomIds(Long[] roomIds)
    {
        Assert.notNull(roomIds,"参数异常");
        for (Long id:roomIds
             ) {
            HotelRoom hotelRoom = hotelRoomMapper.selectHotelRoomByRoomId(id);
            Assert.notNull(hotelRoom,"参数异常，不存在这个房间号"+hotelRoom.getRoomNumber());

            HotelReserve hotelReserve = new HotelReserve();
            hotelReserve.setRoomNumber(hotelRoom.getRoomNumber());
            List<HotelReserve> hotelReserves = hotelReserveMapper.selectHotelReserveList(hotelReserve);
            Long[] longArray = new Long[hotelReserves.size()];
            for(int i = 0; i< longArray.length; i++){
                longArray[i] = hotelReserves.get(i).getReserveId();
            }
            hotelReserveMapper.deleteHotelReserveByReserveIds(longArray);
        }
        return hotelRoomMapper.deleteHotelRoomByRoomIds(roomIds);
    }

    /**
     * 删除房间信息
     * 
     * @param roomId 房间主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHotelRoomByRoomId(Long roomId)
    {
        Assert.notNull(roomId,"参数异常");
        HotelRoom hotelRoom = hotelRoomMapper.selectHotelRoomByRoomId(roomId);
        Assert.notNull(hotelRoom,"该房间不存在");
        HotelReserve hotelReserve = new HotelReserve();
        hotelReserve.setRoomNumber(hotelRoom.getRoomNumber());
        List<HotelReserve> hotelReserves = hotelReserveMapper.selectHotelReserveList(hotelReserve);
        Long[] longArray = new Long[hotelReserves.size()];
        for(int i = 0; i< longArray.length; i++){
            longArray[i] = hotelReserves.get(i).getReserveId();
        }
        hotelReserveMapper.deleteHotelReserveByReserveIds(longArray);
        return hotelRoomMapper.deleteHotelRoomByRoomId(roomId);
    }
}
